package com.ch.doudemo.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by pc on 2016/9/6.
 */
public class StringUtil {
    public static boolean isEmpty(Object str) {
        return str == null || "".equals(str) || "null".equals(str) || " ".equals(str) || "  ".equals(str) || "  ".equals(str);
    }

    public static boolean isZero(Object str) {
        String thisStr = str.toString();
        switch (thisStr) {
            case "0":
            case "00":
            case "000":
            case "0000":
            case "0.0":
            case "0.00":
            case "0.000":
            case "0.0000":
                return true;

        }
        return false;
    }

    public static boolean isEmptyAndZero(Object str) {
        if (str == null || "".equals(str) || "null".equals(str) || " ".equals(str) || "  ".equals(str) || "  ".equals(str)) {
            return true;
        } else {
            String thisStr = str.toString();
            switch (thisStr) {
                case "0":
                case "00":
                case "000":
                case "0000":
                case "0.0":
                case "0.00":
                case "0.000":
                case "0.0000":
                    return true;

            }
            return false;
        }
    }


    /**
     * 正则 是数字就行   小数点前后都有
     * @param str
     * @return
     */
    public static boolean isNumber(String str) {
        if (StringUtil.isEmpty(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    public static boolean isInt(String str) {
        if (StringUtil.isEmpty(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^-?[1-9]\\d*$");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    public static double parseDouble(String str) {
        if (isNumber(str)) {
            return Double.parseDouble(str);
        } else {
            return 0.00;
        }
    }
    public static double parseInt(String str) {
        if (isInt(str)) {
            return Integer.parseInt(str);
        } else {
            return 0;
        }
    }

}
